
const data = { name: '张三' }
// const data = { name: '张三', age: 20, address: '北京市' }

function checkData(data) {
  [
    { column: 'name', label: '姓名', required: true },
    { column: 'age', label: '年龄', required: true },
    { column: 'address', label: '地址' },
  ].forEach((rule) => {
    if (rule.required && !data[rule.column]) {
      throw new Error(`缺少${rule.label}字段`)
    }
  })
}

try {
  checkData(data)
  console.log('数据校验通过')
} catch (error) {
  console.log(error)
}